package com.gz.university.crm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gz.university.crm.dao.ISysPopedomPrivilegeDao;
import com.gz.university.crm.dao.impl.SysPopedomPrivilegeDaoImpl;
import com.gz.university.crm.entity.SysPopedomPrivilege;
import com.gz.university.crm.service.ISysPopedomPrivilegeService;

@Service(value = ISysPopedomPrivilegeService.SERVICE_NAME)
@Transactional(readOnly = true)
public class SysPopedomPrivilegeServiceImpl implements
		ISysPopedomPrivilegeService {
	private ISysPopedomPrivilegeDao sysPopedomPrivilegeDao;

	public ISysPopedomPrivilegeDao getSysPopedomPrivilegeDao() {
		return sysPopedomPrivilegeDao;
	}

	@Resource(name = ISysPopedomPrivilegeDao.DAO_NAME)
	public void setSysPopedomPrivilegeDao(
			ISysPopedomPrivilegeDao sysPopedomPrivilegeDao) {
		this.sysPopedomPrivilegeDao = sysPopedomPrivilegeDao;
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
	@Override
	public void updatePopedom(Integer roleId, String[] popedomModules) {
		// TODO Auto-generated method stub
		// 删除该权限组对应的所有的权限
		if (roleId != null) {
			String hql = "from SysPopedomPrivilege s where s.roleId = :roleId";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("roleId", roleId);
			List<SysPopedomPrivilege> list = this.sysPopedomPrivilegeDao.find(
					hql, params);
			for (SysPopedomPrivilege privilege : list) {
				this.sysPopedomPrivilegeDao.delete(privilege);
			}
		}
		// 保存权限组新的权限
		if (roleId != null && popedomModules != null
				&& popedomModules.length > 0) {

			for (String str : popedomModules) {
				if (StringUtils.isNotBlank(str)) {
					System.out.println("save -----" + roleId + "," + str);
					String[] datas = str.split(",");
					SysPopedomPrivilege sysPopedomPrivilege = new SysPopedomPrivilege();
					sysPopedomPrivilege.setRoleId(roleId);
					sysPopedomPrivilege.setPopedomModule(datas[0]);// 设置模块名称
					sysPopedomPrivilege.setPopedomPrivilege(datas[1]);
					this.sysPopedomPrivilegeDao
							.save(sysPopedomPrivilege);
				}
			}
		}
	}

	@Override
	public List<SysPopedomPrivilege> findSysPopedomPrivilegesByRoleId(
			Integer roleId) {
		// TODO Auto-generated method stub
		final String hql = "from SysPopedomPrivilege s where s.roleId is :roleId";
		Map<String, Object> params = new HashMap<>();
		params.put("roleId", roleId);
		return this.sysPopedomPrivilegeDao.find(hql, params);
	}

	@Override
	public List<SysPopedomPrivilege> findAllSysPopedomPrivilege() {
		// TODO Auto-generated method stub
		final String hql = "from SysPopedomPrivilege s order by s.roleId";
		return this.sysPopedomPrivilegeDao.find(hql);
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
	@Override
	public void deletePopedomPrivilegesByRoleId(Integer roleId,
			String[] popedomModules) {
		// TODO Auto-generated method stub
		// 删除该权限组对应的所有的权限
		if (roleId != null && popedomModules != null
				&& popedomModules.length > 0) {
			String hql = "delete SysPopedomPrivilege s where s.roleId = :roleId";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("roleId", roleId);
			this.sysPopedomPrivilegeDao.executeHqlByParams(hql, params);
		}
	}

}
